<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

    private $_acl = null;
    private $_auth = null;

    protected function _initBaseUrl() {
        $this->bootstrap("frontController");
        $front = $this->getResource("frontController");
        $request = new Zend_Controller_Request_Http();
        $front->setRequest($request);
    }

    protected function _initJQuery() {

        $this->bootstrap('view');
        $view = $this->getResource('view');
        $view->addHelperPath('ZendX/JQuery/View/Helper', 'ZendX_JQuery_View_Helper');
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
        $viewRenderer->setView($view);

        //ZendX_JQuery_View_Helper_JQuery::enableNoConflictMode();
        // jQuery
        $view->JQuery()->addStyleSheet($view->baseUrl() . '/js/jquery/themes/ui-lightness/jquery-ui-1.8.16.custom.css');
        $view->JQuery()->setUiLocalPath($view->baseUrl() . '/js/jquery/ui/jquery-ui-1.8.16.custom.js');
        //jquery-ui-i18n.js

        $view->jQuery()->enable();
        $view->jQuery()->uiEnable();
    }

    protected function _initTranslate() {

        $locale = new Zend_Locale('fr_FR'); //'fr_FR'
        Zend_Registry::set('locale', $locale);
        $translate = new Zend_Translate('array', '../languages/fr/fr.php', 'fr');
        $translate->setLocale('fr_FR');
        Zend_Registry::set('Zend_Translate', $translate);
        return $translate;
    }

    protected function _initView() {

        // Initialize view
        $view = new Zend_View();
        $view->doctype('HTML5');
        $view->headMeta()->appendHttpEquiv('Content-Type', 'text/html; charset=utf-8');
        $view->headTitle()->setSeparator(' - ');
        $view->headTitle('Cabinet');

        $view->headLink()->prependStylesheet($view->baseUrl() . '/css/twistter/bootstrap.css')
                ->appendStylesheet($view->baseUrl() . '/css/twistter/docs/assets/css/docs.css')
                ->appendStylesheet($view->baseUrl() . '/css/twister-custom.css');

        // tablesorter.com/docs/index.html
        $view->headScript()->appendFile($view->baseUrl() . '/js/jQuery/jquery.tablesorter.min.js')
                ->appendFile($view->baseUrl() . '/js/jQuery/ui/i18n/jquery-ui-i18n.js')
                ->appendFile("http://maps.googleapis.com/maps/api/js?key=AIzaSyDFnDa3JOTMdjMEZOYwepTMf_cRBYYjAps&sensor=true");

        return $view;
    }

    protected function _initAcl() {

        $this->bootstrap('frontController');
        $frontController = $this->getResource('frontController');
        $this->_acl = new Cab_Acl_Acl();
        $this->_auth = Zend_Auth::getInstance();

        if (!$this->_auth->hasIdentity()) {
            $this->_auth->getStorage()->read()->role = 'guest';
        }

        // $auth est une référence vers Zend_Auth (getInstance())
        $frontController->registerPlugin(new Cab_Controller_Plugin_Acl($this->_acl, $this->_auth));
    }

    protected function _initNavigation() {
        $this->bootstrap('layout');
        $layout = $this->getResource('layout');
        $view = $layout->getView();

        $navConfig = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml', 'nav');
        $navigation = new Zend_Navigation($navConfig);

        // Zend_Debug::dump($this->_auth->getStorage()->read()->role);
        $view->navigation($navigation)->setAcl($this->_acl)
                ->setRole($this->_auth->getStorage()->read()->role);
    }

    protected function _initZFDebug() {

        if ($this->hasOption('zfdebug')) {
            $zfdebug = $this->getOption('zfdebug');

            if ($zfdebug['enabled'] == 1) {

                $options = array(
                    'jquery_path' => '', /* desactive le lien vers jQuery */
                    'plugins' => array('Variables',
                        'File' => array('base_path' => realpath(APPLICATION_PATH . '/../')),
                        //'Memory',
                        'Html',
                        //'Time',
                        'Registry',
                        'Exception'));

                # Instantiate the database adapter and setup the plugin.
                # Alternatively just add the plugin like above and rely on the autodiscovery feature.
                if ($this->hasPluginResource('db')) {
                    $this->bootstrap('db');
                    $db = $this->getPluginResource('db')->getDbAdapter();
                    $options['plugins']['Database']['adapter'] = $db;
                }

                # Setup the cache plugin
                if ($this->hasPluginResource('cachemanager')) {
                    $this->bootstrap('cachemanager');
                    $cache = $this->getPluginResource('cachemanager')->getCacheManager();

                    $options['plugins']['Cache']['backend'] = $cache->getCache('database')->getBackend();

                    # set up database meta data cache
                    Zend_Db_Table_Abstract::setDefaultMetadataCache($cache->getCache('database'));
                }

                # Setup the cache plugin
                if ($this->hasPluginResource('cache')) {
                    $this->bootstrap('cache');
                    $cache = $this - getPluginResource('cache')->getDbAdapter();
                    $options['plugins']['Cache']['backend'] = $cache->getBackend();
                }

                $debug = new ZFDebug_Controller_Plugin_Debug($options);

                $this->bootstrap('frontController');
                $frontController = $this->getResource('frontController');
                $frontController->registerPlugin($debug);
            }
        }
    }

}
